package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbComplaint;
import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.services.ComplaintService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginCustomer;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Classname ComplaintController
 * @Date 2024/1/14 17:17
 */
@RestController
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;
    @PostMapping("/complaint/selAll")
    public ResultVO getComplaints(@RequestBody Map<String,Object> map){
        Integer pageSize = (Integer) map.get("pageSize");
        Integer pageNum = (Integer) map.get("pageNum");
        PageInfo<TbComplaint> pageInfo = complaintService.getAllComplaints(pageNum, pageSize);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("error");
    }

    @PostMapping("/complaint/delComplaint/{tcid}")
    public ResultVO softdelete(@PathVariable("tcid") Integer tcid){
        int i = complaintService.updateSoftdel(tcid);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");
    }
    @PostMapping("/complaint/change/{tcid}")
    public ResultVO changeComplaint(@PathVariable("tcid") Integer tcid){
        int i = complaintService.updateComplaint(tcid);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");
    }

    @PostMapping("/complaint/condition")
    public ResultVO getCustomersByCondition(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String beginTimeStr = (String) map.get("beginTime");
        String endTimeStr = (String) map.get("endTime");
        Date beginTime = new Date();
        Date endTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(beginTimeStr!="" && beginTimeStr!=null){
            try {
                beginTime = simpleDateFormat.parse(beginTimeStr);
                endTime = simpleDateFormat.parse(endTimeStr);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            PageInfo<TbComplaint> pageInfo = complaintService.getByCondition(pageNum,pageSize,beginTime,endTime);
            return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("failed");
        }else{
            return null;
        }

    }

    @PostMapping("/complaint/selByCid")
    public ResultVO getComplaintsByCid(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Integer cid = (Integer) map.get("cid");
        PageInfo<TbComplaint> pageInfo = complaintService.getComplaintsByCid(pageNum,pageSize,cid);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("failed");
    }

    /**
     * 客户反馈
     */
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/customerComplaint/save")
    public ResultVO saveComplaint(@RequestBody Map<String,Object> map){
        String detail = (String) map.get("detail");
        ServletRequestAttributes servletRequestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String customerName = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginCustomer loginCustomer = (LoginCustomer) valueOperations.get("customer:"+customerName);
        Integer cid = loginCustomer.getCustomer().getCid();
        map.put("cid",cid);
        TbComplaint tbComplaint = new TbComplaint();
        tbComplaint.setCid(cid);
        tbComplaint.setDetail(detail);
        tbComplaint.setComplaintTime(new Date());
        tbComplaint.setFlag(1);//1为待处理 2处理中 3已处理
        tbComplaint.setBeiyong1("1");//"1"为软删除
        int i = complaintService.saveOne(tbComplaint);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

    /**
     * 客户界面实现反馈记录软删除
     */

    @GetMapping("/customerComplaint/softDelOne/{tcid}")
    public ResultVO delOneByTcid(@PathVariable("tcid") Integer tcid){
        TbComplaint tbComplaint = new TbComplaint();
        tbComplaint.setTcid(tcid);
        tbComplaint.setBeiyong1("0");
        int i = complaintService.softdel(tbComplaint);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }
}
